'''
Author: @zhyy
Date: 2021-01-27 14:23:32
LastEditors: @zhyy
LastEditTime: 2021-02-07 17:47:32
'''
from flask import request, jsonify,session,g

import time,os,json
from flask import current_app,jsonify,g
from app.middlewares.auth_required import login_required
from app.models.model import User,Role
from app.models.model_schema import Userchema,Rolechema
from app.dao.user import UserDB
from app.dao.base import BaseModel
from app.dao.query_to_json import QueryToJson
from app.dao.db_query import DBQuery
from app.api.main import main
from app.func import error_code as RET
from app import db


from app.tasks.task import add
from app.cache.redis_cache import celery_broker_result


@main.route("/test")
def celery_test():
    result = add.delay(14, 20)
    return jsonify(code=0,msg='ok',result=str(result))

@main.route("/test/<task_id>")
def t_s(task_id):
    s=add.AsyncResult(task_id)
    result=celery_broker_result.get("celery-task-meta-{}".format(task_id))
    result=json.loads(result)
    return jsonify(code=0,result=result)

@main.route("/")
def index():
    return "index"



@main.route("/user/info",methods=["GET"])
# @login_required
def get_user_info():
    # filters={
    #     User.role_id==1
    # }
    # u=UserDB.get_many(User,filters=filters,chema=Userchema)
    # filters={    
    # }
    # s=BaseModel.get_many(Role,filters=filters,chema=Rolechema)
    # u=db.session.query(User.id,User.name,Role.id,Role.role).filter(User.role_id==1).all()
    # print(u)
    # keys=["id","uname","role_id","role"]
    # data=query_Single_table_to_json_list_by_field(keys,u)
    # u=db.session.query(User.id,User.name,Role.id,Role.role).filter(User.role_id==1).first()
    # print(u)
    # keys=["id","uname","role_id","role"]
    # data=query_Single_table_to_json_by_field(keys,u)
    # ----------------------------------------
    # 查询指定字段
    # d=[]
    # keys=["id","role_name"]
    # for s in u:
    #     u= {k:v for k,v in zip(keys,s)}
    #     d.append(u)
    # print(d)
    # ------------------------------------------
    # 查询整张表
    # u=db.session.query(User).filter(User.role_id==Role.id).all()
    # s=QueryToJson.query_Single_table_to_json_list(u)
    # print(s)
    
    # page_data = db.session.query(User.id,User.name,Role.role).filter(User.role_id==Role.id).paginate(
    #             page=1, per_page=10, error_out=False)
    # print(page_data)
    # keys=["id","name","role"]
    # d=QueryToJson.query_page_field(keys=keys,result=page_data)
    # print(d)
    # page_data = db.session.query(User).filter(User.role_id==1).paginate(
    #             page=1, per_page=10, error_out=False)
    # print(page_data)
    # d=QueryToJson.query_page_Single_table(page_data)
    # print(d)
    # field={"uid":User.id,"role_id":Role.id}
    # k_f={"id":User.id,"name":User.name,"role":Role.role}
    # s=db.session.query(*list(k_f.values())).all()
    # print(s)
    # filters={
    #     User.role_id==1
    # }
    # d=QueryToJson.get_filed_page(keys_field=k_f,filters=filters,page=1,per_page=5)
    # print(d)
    # try:
    #     id=db.session.query(User.id).filter(User.name=="zhangsan").first()
    # except Exception as e:
    #     print(repr(e))
    # print(id.id)
    
    # 获取id
    field=User.id
    filters={User.name=="zhangsan"}
    id=DBQuery.get_id(field=field,filters=filters)
    print("id:",id)

    # 获取user表全部字段一条记录
    filters={User.role_id==1}
    one_user=DBQuery.get_one_use_tb(_cls=User,filters=filters)
    print("one tb:",one_user)

    # 获取user表指定字段一条记录
    keys=["id","name","role_id","role"]
    field=[User.id,User.name,Role.id,Role.role]
    filters={User.role_id==Role.id,User.role_id==2}
    one_user_name=DBQuery.get_one_use_field(keys,field,filters)
    print("one field:",one_user_name)

    # 获取user表全部字段所有记录
    filters={User.role_id==Role.id,User.role_id==2}
    user_tb=DBQuery.get_all_use_tb(_cls=User,filters=filters)
    print("all tb:",user_tb)

    # 获取user表指定字段所有记录
    keys=["id","name","role_id","role"]
    field=[User.id,User.name,Role.id,Role.role]
    filters={User.role_id==Role.id,User.role_id==2}
    user_all_field=DBQuery.get_all_use_field(keys,field,filters)
    print("all field:",user_all_field)

    # 获取user表所有字段分页数据
    filters={User.role_id==Role.id,User.role_id<4}
    user_page_tb=DBQuery.get_page_use_tb(_cls=User,filters=filters,page=1,per_page=5)
    print("page tb:",user_page_tb)

    # 获取user表指定字段分页数据
    k_f={"id":User.id,"name":User.name,"role_id":Role.id,"role":Role.role}
    user_page_field=DBQuery.get_page_use_field(keys_field=k_f,filters=filters,page=1,per_page=5)
    print("page field:",user_page_field)
    return jsonify(code=RET.Success.json_msg(data=user_page_field))
    


# 获取表单提交的图片
@main.route('/get_img',methods=['POST'])
def get_img():
	# 通过表单中name值获取图片
    imgData = request.files["image"]
    # 设置图片要保存到的路径
     #获取项目当前绝对路径
    # 比如我的项目在桌面上存放则得到——"C:\Users\asus\Desktop\shou"
    basedir = os.path.abspath(os.path.dirname(__file__))
    path = basedir + "/static/upload/"
    print(path)
    # 获取图片名称及后缀名
    imgName = imgData.filename

    # 图片path和名称组成图片的保存路径
    file_path = path + imgName

    # 保存图片
    imgData.save(file_path)

    # url是图片的路径
    url = '/static/upload/img/' + imgName
    print(url)
    
    return 'ok'
